﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SaturniLibrary.Cache
{
    public abstract class CacheBase : ICache
    {
        protected CachePolicy GetCachePolicy(string groupName)
        {
            return CachePolicyManager.Get(groupName);
        }

        protected abstract ICacheAPI GetCacheApi(string groupName);


        #region ICache Members

        public void Add(string groupName, string key, object value)
        {
            ICacheAPI cacheApi = GetCacheApi(groupName);
            CachePolicy cachePolicy = GetCachePolicy(groupName);
            cacheApi.Add(key, value, cachePolicy.ExpiredSeconds);
        }

        public void Remove(string groupName, string key)
        {
            ICacheAPI cacheApi = GetCacheApi(groupName);
            CachePolicy cachePolicy = GetCachePolicy(groupName);
            cacheApi.Remove(key);
        }

        public object Get(string groupName, string key)
        {
            ICacheAPI cacheApi = GetCacheApi(groupName);
            CachePolicy cachePolicy = GetCachePolicy(groupName);
            return cacheApi.Get(key);
        }

        public bool Contains(string groupName, string key)
        {
            ICacheAPI cacheApi = GetCacheApi(groupName);
            CachePolicy cachePolicy = GetCachePolicy(groupName);
            return cacheApi.Contains(key);
        }

        public long Size(string groupName)
        {
            ICacheAPI cacheApi = GetCacheApi(groupName);
            CachePolicy cachePolicy = GetCachePolicy(groupName);
            return cacheApi.Size();
        }

        public void Flush(string groupName)
        {
            ICacheAPI cacheApi = GetCacheApi(groupName);
            CachePolicy cachePolicy = GetCachePolicy(groupName);
            cacheApi.Flush();
        }

        public void Update(string groupName, string key, object value)
        {
            ICacheAPI cacheApi = GetCacheApi(groupName);
            CachePolicy cachePolicy = GetCachePolicy(groupName);
            cacheApi.Update(key, value, cachePolicy.ExpiredSeconds);
        }

        #endregion
    }
}
